<?php
namespace App\Models\Factory;

use App\Models\AbsModelFactory;
use App\Models\Orm\Device;
use App\Models\Orm\User;
use App\Models\Orm\UserCredit;



class ComDeviceFactory extends AbsModelFactory
{
    //获取常用设备
    public static function getComDevice($userId)
    {
       $data = Device::where('user_id',$userId)->get();
        return $data ? $data  : '';
    }

    //删除常用设备
    public static function  delComDevice($id)
    {
        $data['device'] ='';
        $data['user_id'] = 0;
        $data['version_number'] = '';
        $result = Device::where('id',$id)->update($data);

        return $result ? true : false;
    }

    //判断设备是否为常用设备
    public static function is_device($device,$version_number,$id)
    {
        $data = Device::where(['device' => $device,'version_number'=>$version_number,'user_id'=>$id])->first();
        return $data ? true : false;
    }

    //插入新设备到常用设备表
    public static  function addDevice($userId,$device,$version_number)
    {
        $result = Device::firstOrCreate(['device' => $device,'version_number'=>$version_number,'user_id'=>$userId],['device' => $device,'version_number'=>$version_number,'user_id'=>$userId]);
        return $result? true :false;
    }

    public static  function openDevice($user_id,$is_device)
    {
        $data['is_device'] = $is_device;
        $result = User::where('id',$user_id)->update($data);
        return $result ? true :false;
    }
}